/* Includes ------------------------------------------------------------------*/
#include "ad5274_5271.h"
#include "iic_current.h"
//#include "sys.h"
void Dly_10ns (u32 j);
//void TWait(void);


#define DELAY_TIME 3				 //03-15修改为3，之前为6；使用正常；

BYTE AD5274_5271_pucData[2];

void DelayMs(u16 x)
{    
	volatile u32 i,j;	
	for(j=0;j<x;j++){
		for(i=0;i<400;i++)
		{;}
	}
}

void AD5274_5271_I2CSDASetInput(void); // Set I2C SDA signal as input
void AD5274_5271_I2CSDASetOutput(void); // Set I2C SDA signal as output

void AD5274_5271_GPIO_Config(void)
{
	GPIO_InitTypeDef GPIO_Initure;  	
	__HAL_RCC_GPIOA_CLK_ENABLE();               //开启GPI0A时钟	
	__HAL_RCC_GPIOB_CLK_ENABLE();
	__HAL_RCC_GPIOD_CLK_ENABLE();
	__HAL_RCC_GPIOE_CLK_ENABLE(); 	
	
	GPIO_Initure.Pin = VCI_EN;		
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
	HAL_GPIO_Init(VCI_ENPeriph_GPIO,&GPIO_Initure); 	 
	VCI_EN_HIGH;
	
	GPIO_Initure.Pin = VDDIO_EN;		
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull = GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
	HAL_GPIO_Init(VDDIO_ENPeriph_GPIO,&GPIO_Initure); 
	VDDIO_EN_HIGH;
	
	GPIO_Initure.Pin = DVDD_EN;		
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull = GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
	HAL_GPIO_Init(DVDD_ENPeriph_GPIO,&GPIO_Initure); 
	//DVDD_EN_HIGH; //
	DVDD_EN_LOW;
	GPIO_Initure.Pin = AD5274_5271PIN_SCL;		
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
	HAL_GPIO_Init(GPIO_SCL,&GPIO_Initure); 	 
	
	GPIO_Initure.Pin = AD5274_5271PIN_SDA;		
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
	HAL_GPIO_Init(GPIO_SDA,&GPIO_Initure); 	
}

void AD5274_5271_I2CSDASetInput(void)
{
	GPIO_InitTypeDef GPIO_Initure; 	
	GPIO_Initure.Pin = AD5274_5271PIN_SDA;		
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_INPUT;	
	HAL_GPIO_Init(GPIO_SDA,&GPIO_Initure); 
} 
void AD5274_5271_I2CSDASetOutput(void)
{ 
	GPIO_InitTypeDef GPIO_Initure; 	
	GPIO_Initure.Pin = AD5274_5271PIN_SDA;		
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =	GPIO_NOPULL;//iic总线有上拉电阻，无需拉高GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;//GPIO_MODE_OUTPUT_PP;	
	HAL_GPIO_Init(GPIO_SDA,&GPIO_Initure); 
}

// Start bit of I2C waveform
void AD5274_5271_I2CStart(void)
{
    AD5274_5271_I2CSDASetOutput();
    AD5274_5271_TPSDA_H; 
    AD5274_5271_TPSCL_H;
		TWait();
    AD5274_5271_TPSDA_L; 
		TWait();
    AD5274_5271_TPSCL_L; 
}

// Stop bit of I2C waveform
void AD5274_5271_I2CStop(void)
{
    AD5274_5271_I2CSDASetOutput();
    AD5274_5271_TPSCL_H;
    AD5274_5271_TPSDA_L;
    AD5274_5271_TPSCL_H;
    TWait();
    AD5274_5271_TPSDA_H;
    TWait();
}

// Send one byte from host to client
bool AD5274_5271_I2CSendByte(BYTE ucData)
{
	bool bRet;
	unsigned char i=8; 
	AD5274_5271_I2CSDASetOutput();
	do{
		if (ucData&0x80)
			AD5274_5271_TPSDA_H;
			else 
			AD5274_5271_TPSDA_L;
		TWait();//-----------------------????
		AD5274_5271_TPSCL_H;
		TWait();
		ucData<<=1;
		AD5274_5271_TPSCL_L;
			if (i>1) //判断是否传递最后一位数据，若是最后一位则TSCL拉低后不用等待
		TWait();
    }while(--i);
	
	AD5274_5271_I2CSDASetInput();

	bRet=FALSE;
	AD5274_5271_TPSCL_H;
	TWait();
	if (HAL_GPIO_ReadPin(GPIO_SDA, AD5274_5271PIN_SDA))
		bRet=TRUE;
	AD5274_5271_TPSCL_L;
	TWait();
	return bRet;
}
  
// Receive one byte form client to host
BYTE AD5274_5271_I2CReciveByte(bool bAck)
{
	BYTE ucRet,i;
  
	AD5274_5271_I2CSDASetInput();
	ucRet = 0;
	i=8;
	do
	{	AD5274_5271_TPSCL_H;
		TWait();
		ucRet<<=1;
		if(HAL_GPIO_ReadPin(GPIO_SDA, AD5274_5271PIN_SDA))
			ucRet++;
		AD5274_5271_TPSCL_L;
                if (i>1) //判断是否传递最后一位数据，若是最后一位则TSCL拉低后不用等待
		TWait();
	}while(--i);

	AD5274_5271_I2CSDASetOutput();

	if (bAck==0)			//这里可能需要调整
    AD5274_5271_TPSDA_L;
	else AD5274_5271_TPSDA_H;

	AD5274_5271_TPSCL_H;
	TWait();
	AD5274_5271_TPSCL_L;
	TWait();         
	return ucRet;
}

// I2C send data fuction
bool AD5274_5271_I2CSend(BYTE ucDeviceAddr, BYTE ucIndex, u16 RDACValue)
{
      bool bRet = TRUE;

      AD5274_5271_I2CStart();

      if(AD5274_5271_I2CSendByte(ucDeviceAddr & 0xFE) == ACK)
        {
          ucIndex = (ucIndex<<2) + ((RDACValue>>8)&0x03);

		    if(AD5274_5271_I2CSendByte(ucIndex) == ACK)
            {
                  if(AD5274_5271_I2CSendByte(RDACValue&0xff) == NAK)
                  {
                    bRet = FALSE;
                  }
            }
          else
            {
             bRet = FALSE;
            }
        }
      else
      {
      	bRet = FALSE;
      }
      AD5274_5271_I2CStop();
      return bRet;
}
// I2C receive data function
bool AD5274_5271_I2CReceive(BYTE ucDeviceAddr, BYTE ucIndex, BYTE* AD5274_5271_pucData, unsigned int unDataLength) //(器件地址，buffer类型，被读出数据保存位置，读出数据长度)
{
    unsigned int i;
    bool bRet = TRUE;
    AD5274_5271_I2CStart();
    if(AD5274_5271_I2CSendByte(ucDeviceAddr & 0xFE) == ACK)//FRAME 1 SERIAL BUS ADDRESS BYTE   WRITE 0
        {
			ucIndex = (ucIndex<<2);
				//	+ AD5274_5271_pucData[0];
			if(AD5274_5271_I2CSendByte(ucIndex) == ACK)   //FRAME 2 MOST SIGNIFICANT DATA BYTE
			{
			   if(AD5274_5271_I2CSendByte(AD5274_5271_pucData[0]) == ACK)  //FRAME 3 LEAST SIGNIFICANT DATA BYTE
				{
					AD5274_5271_I2CStop();		 
					AD5274_5271_I2CStart();
					if(AD5274_5271_I2CSendByte(ucDeviceAddr | 0x01) == ACK)   //READ 1
					{
						for(i = 0; i < unDataLength-1; i++)
						{  AD5274_5271_pucData[i] = AD5274_5271_I2CReciveByte(ACK);
						}
						AD5274_5271_pucData[unDataLength - 1] = AD5274_5271_I2CReciveByte(NAK);
					}
					else
					{          bRet = FALSE;           }
				}
				else
				{          bRet = FALSE;           }
			}
			else
			{           bRet = FALSE;           }
		}
		else
        {           bRet = FALSE;           }
    AD5274_5271_I2CStop();
    return bRet;
}

void VDD_AD5274_5271_Init(u16 uvol)
{	
		
	DelayMs(10);
	AD5274_5271_I2CSend(DEVICE_ADDRESS_VDD, COMMAND_WRITE_CONTROL_INDEX, 3);
	DelayMs(10);	
	AD5274_5271_I2CSend(DEVICE_ADDRESS_VDD, COMMAND_WRITE_RDAC_INDEX, uvol);	
	DelayMs(10);		
}

void VDDIO_AD5274_5271_Init(u16 uvol)
{	
	//AD5274_5271_GPIO_Config();		
	DelayMs(10);
	AD5274_5271_I2CSend(DEVICE_ADDRESS_VDDIO, COMMAND_WRITE_CONTROL_INDEX, 3);
	DelayMs(10);	
	AD5274_5271_I2CSend(DEVICE_ADDRESS_VDDIO, COMMAND_WRITE_RDAC_INDEX, uvol);
	DelayMs(10);		
}
void DVDD_AD5274_5271_Init(u16 uvol)
{
	DelayMs(10);
	AD5274_5271_I2CSend(DEVICE_ADDRESS_DVDD, COMMAND_WRITE_CONTROL_INDEX, 3);
	DelayMs(10);	
	AD5274_5271_I2CSend(DEVICE_ADDRESS_DVDD, COMMAND_WRITE_RDAC_INDEX, uvol);
	DelayMs(10);
}
